package com.arges.controller;

import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * TODO
 * </p>
 *
 * @author fanweitao
 * @date 2023/02/14
 */
public class Test8 {


    public static void main(String[] args) {
//        MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
//
//        form.add("test","test1");
//        form.add("test","test2");
//        form.add("test","test3");
//        form.add("test","test4");
//        form.add("test","test5");
//        System.out.println(form);
//        System.out.println("==============");
//        Map<String, Object> map = new HashMap<>();
//        map.put("test","test1");
//        map.put("test","test2");
//        System.out.println(map);
        test();
    }
    private static void test(){
        try{
        /*
        Base64:java.util
        PKCS8EncodedKeySpec:java.security
        KeyFactory:java.security
        Signature:java.security
         */
            //需要签名字符串
            String jsonString= "{\"appid\":\"58c8793ad66511e89e060050569d3255\",\"md5\":\"cb88af352e8871802ec4ce60162df99d\",\"timestamp\":\"231545134555\",\"version\":\"1.0\"}";
            //RSA密钥
            String privateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAM1BEzOAMVv5aTgM7khzKdcuqM7GjCPQtAoiizWLb924KIAJuGPvJhRMZowa83+4tsYmsdPt+zhOFr6OdsIhfaSHcYAo/N/ehHaot1pO54fWYbIRpuwegj54w+qMdZLxMhzRHy5ZA53j47SVwblSebRGTGpbCRf41xArxdUs+xV3AgMBAAECgYBYn3Vz8jDdvoXw3pxwCdGSCKJ4jX/q4u0pxxqGZwtJF3/dMNU9yP+PGiHY8rYaep3oVsLHd+TVvdnSM18RmTZO8hta51iBcs+zWy7PqY0GiGXNvBC3y3OBOKb49FvXawERzsY7adTmWyHNyS2RGBF7ZGbUPJVPSJiAR5vknOfogQJBAPvnUM+JDZhUzjQRkOyx5KWYT88H1LWXDS5zTTsdqhzBAJc2gjEVVCRL5aWtQS/3q/X++Oo2+wbmp0lTKXKgZjcCQQDQl45hdtqczxAUcP/aCdmIDMqCQNL1oaTtIU/WTrX4N8gjAHR/uBPPfvmxkA5zx7B8ywthAslclfpigVMkfyrBAkEAnY16D7Pq2uH/7rUl7cT9+0yebiC5u7H0Pp9DKLxPD5jvY6RmHYj3jZQi2FLauMOxvDRzPxqQUyq8arIm9Fi6VQJAPbsMbPWintN8m8ARR7KwiN3YNJIAnKbYy0CXgwHKQoonlYw17fJJEpguRwkt7b/EEDp6xJvxgY/1CJ/jPiLQQQJBAO5e2zjoZgM7glOWoz/ks9Kcu9uW8WDevs8JYyRDnxQVKDcNUlfzH3tTgLhuWco3zr2XyDMzrbUvrvOF40LaGzc=";
            Base64.Encoder encoder = Base64.getEncoder();
            Base64.Decoder decoder = Base64.getDecoder();
            PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(decoder.decode(privateKey.getBytes()));
            PrivateKey priKey = KeyFactory.getInstance("RSA").generatePrivate(priPKCS8);
            Signature signature = Signature.getInstance("SHA1WithRSA");
            signature.initSign(priKey);
            signature.update(jsonString.getBytes(StandardCharsets.UTF_8));
            byte[] sign = signature.sign();
            String s = encoder.encodeToString(sign);
            System.out.println(s);
            String signarureString = encoder.encodeToString(sign);
            System.out.println("signarureString=" + signarureString);
        }
        catch (Exception e){
           e.printStackTrace();
        }
    }
}
